package org.biouno.unochoice;

import com.alibaba.fastjson.JSONObject;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.util.ListBoxModel;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Pattern;

import static hudson.Util.fixEmpty;
import static org.biouno.unochoice.util.Utils.getNodes;

@ExportedBean
public class MultipleChoice extends AbstractDescribableImpl<MultipleChoice> implements Serializable {

    private String nodeLabels;



    @DataBoundConstructor
    public MultipleChoice(String nodeLabels) {
        this.nodeLabels = nodeLabels;
    }


    @Exported
    @Whitelisted
    public String getNodeLabels() {
        return nodeLabels;
    }





    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        return jsonObject.toString();
    }


    public String nodeLabelsToString() {
        return JSONObject.toJSONString(nodeLabels);
    }

    private final static Pattern SCP_LIKE = Pattern.compile("(.*):(.*)");


    @Extension
    public static class DescriptorImpl extends Descriptor<MultipleChoice> {

        public ListBoxModel doFillNodeLabelsItems(@AncestorInPath Item project) {

            ListBoxModel listBoxModel = new ListBoxModel();
            getNodes().forEach(node -> {
                listBoxModel.add(node);
            });
            return listBoxModel;
        }
        @Override
        public String getDisplayName() {
            return "";
        }
    }
}
